const $ = ele => document.querySelector(ele);


const ajax = options => {
    const {
        // 数据请求方式
        type,
        // 后端返回的数据类型  dataType  默认为json
        dataType = 'json',
        // 前端携带的参数  data  默认为{}
        data = {},
        successCB,
        path,
        async = true
    } = options;

    // 把数据格式改成 key1=v1&key2=v2 
    let params = '';    // params 参数
    for (let key in data) {
        params += `${key}=${data[key]}&`;
    }
    params = params.substring(0, params.length - 1);

    // 做请求

    const xhr = new XMLHttpRequest();
    // 区分请求方式
    if (type.toLowerCase() === 'get') {
        xhr.open('get', path + '?' + params, async);
        xhr.send()
    }
    else if (type.toLowerCase() === 'post') {
        xhr.open('post', path, async);
        // 设置请求头，为了让服务端正确的解析数据格式
        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xhr.send(params);
    }
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            let res = xhr.responseText;
            // 如果是json格式，就直接转一下
            if (dataType.toLowerCase() === 'json') {
                res = JSON.parse(res);
            }
            successCB(res);
        }
    }
}




const request = (type , data , path) => {
    return new Promise(resolve => {
        ajax({
            type,
            data,
            path,
            successCB : res => {
                resolve(res);
            }
        })
    })
}


const register = data => request('post' , data , 'http://localhost/Second_php/second/2112Nike/php/myRegister.php');

const login = data => request('post' , data , 'http://localhost/Second_php/second/2112Nike/php/myLogin.php');

const myList = data => request('get' , data , 'http://localhost/Second_php/second/2112Nike/php/myList.php');

const details = data => request('post' , data , 'http://localhost/Second_php/second/2112Nike/php/myDetails.php');

const addToCart = data => request('post' , data , 'http://localhost/Second_php/second/2112Nike/php/myAddToCart.php');

const myCart = data => request('post' , data , 'http://localhost/Second_php/second/2112Nike/php/myCart.php');

const myPersonalCenter = data => request('post' , data , 'http://localhost/Second_php/second/2112Nike/php/personal_center.php');

const myUpdate = data => request('post' , data , 'http://localhost/Second_php/second/2112Nike/php/update.php');

const myDelete = data => request('post' , data , 'http://localhost/Second_php/second/2112Nike/php/myDelete.php');



// const register = data => request('post' , data , '../php/myRegister.php');

// const login = data => request('post' , data , '../php/myLogin.php');

// const myList = data => request('get' , data , '../php/myList.php');

// const details = data => request('post' , data , '../php/myDetails.php');

// const addToCart = data => request('post' , data , '../php/myAddToCart.php');

// const myCart = data => request('post' , data , '../php/myCart.php');

// const myPersonalCenter = data => request('post' , data , '../php/personal_center.php');

// const myUpdate = data => request('post' , data , '../php/update.php');

// const myDelete = data => request('post' , data , '../php/myDelete.php');